package com.lz.autotest.common.entity;

import cn.hutool.json.JSONUtil;
import com.lz.autotest.common.constant.HttpStatus;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: B946
 * @Date: 04/03/2020 10:28
 * @Description:
 */
public class ResponseVo {

    //结果标识 0：正常  其他：异常
    @Getter
    private Integer code;

    //错误信息
    @Setter
    @Getter
    private String msg;
    @Setter
    @Getter
    private boolean success;

    //返回结果
    @Setter
    @Getter
    private Object result;

    //自定义返回参数2
    @Setter
    @Getter
    private String sole;



    public void setCode(Integer code) {
        if (code != HttpStatus.SUCCESS) {
            this.setSuccess(false);
        }
        this.code = code;
    }


    /**
     * 使用成功表示和返回的对象创建通用json格式。
     */
    public ResponseVo(boolean success, Object msg) {
        super();
        if (success) {
            this.code = HttpStatus.SUCCESS;
            this.success = true;
            if (msg != null && StringUtils.isNotBlank(msg.toString())) {
                this.result = msg;
            }
        } else {
            this.code =HttpStatus.ERROR;
            this.success = false;
            if (msg != null && StringUtils.isNotBlank(msg.toString())) {
                this.msg = (String) msg;
            }
        }
    }

    /**
     * 使用成功表示和返回的对象创建通用json格式。
     */
    public ResponseVo() {
        this.code = HttpStatus.SUCCESS;
        this.success = true;
        this.msg = "";
        this.result = null;
    }

    public ResponseVo(boolean success, Object... args) {
        super();

        if (success) {
            this.code = HttpStatus.SUCCESS;
            this.setSuccess(true);
            Map<String, Object> map = new HashMap<>();
            if (args != null && args.length > 0) {
                for (int i = 0; i < args.length - 1; i++) {
                    if (i % 2 == 0) {
                        if (args[i] != null && args[i + 1] != null) {
                            map.put(args[i].toString(), args[i + 1]);
                        }
                    }
                }
                this.result = map;
            }
        } else {
            this.code = HttpStatus.ERROR;
            this.setSuccess(false);
            Map<String, Object> map = new HashMap<>();
            if (args != null && args.length > 0) {
                for (int i = 0; i < args.length - 1; i++) {
                    if (i % 2 == 0) {
                        if (args[i] != null && args[i + 1] != null) {
                            map.put(args[i].toString(), args[i + 1]);
                        }
                    }
                }
                this.msg = JSONUtil.toJsonStr(map);
            }
        }


    }

    public static ResponseVo success() {
        return new ResponseVo();
    }

    public static ResponseVo success(Object result) {
        ResponseVo rv = new ResponseVo();
        rv.setCode(HttpStatus.SUCCESS);
        rv.setMsg("");
        rv.setSuccess(true);
        rv.setResult(result);
        return rv;
    }


    public static ResponseVo failure(String msg) {
        ResponseVo rv = new ResponseVo();
        rv.setCode(HttpStatus.ERROR);
        rv.setSuccess(false);
        rv.setMsg(msg);
        return rv;
    }

    public static ResponseVo failure(String msg, Integer code) {
        ResponseVo rv = new ResponseVo();
        rv.setCode(code);
        rv.setSuccess(code == HttpStatus.SUCCESS);
        rv.setMsg(msg);
        return rv;
    }

    public static ResponseVo failure(Integer code) {
        ResponseVo rv = new ResponseVo();
        rv.setCode(code);
        rv.setSuccess(false);
        rv.setMsg(code + "");
        return rv;
    }

    public static ResponseVo success(Object... args) {
        if (args.length == 1) {
            return new ResponseVo(true, args[0]);
        }
        return new ResponseVo(true, args);
    }

    public static ResponseVo failure(Object... args) {
        if (args.length == 1) {
            return new ResponseVo(false, args[0]);
        }
        return new ResponseVo(false, args);
    }

}
